/*
 * Copyright (C) 2009 Rob Cash
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * RemoteFileLocationEditor.java
 * 
 * Created on Jan 18, 2009 at 1:38:19 PM
 */

package org.robcash.apps.messagepopup.view.components;

import org.robcash.commons.swing.wizard.WizardComponent;

/**
 * Editor for remote file location
 * @author Rob
 */
public class NameEditor extends WizardComponent {

    private boolean nameValidBeforeEditing = false;

    /** Creates new form RemoteFileLocationEditor */
    public NameEditor() {
        initComponents();
        errorLabel.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        instruction = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        errorLabel = new javax.swing.JLabel();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(org.robcash.apps.messagepopup.Main.class).getContext().getResourceMap(NameEditor.class);
        instruction.setText(resourceMap.getString("instruction.text")); // NOI18N
        instruction.setName("instruction"); // NOI18N

        nameTextField.setText("http://"); // NOI18N
        nameTextField.setName("nameTextField"); // NOI18N
        nameTextField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                nameTextFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                nameTextFieldFocusLost(evt);
            }
        });

        errorLabel.setForeground(resourceMap.getColor("errorLabel.foreground")); // NOI18N
        errorLabel.setLabelFor(nameTextField);
        errorLabel.setText(resourceMap.getString("errorLabel.text")); // NOI18N
        errorLabel.setName("errorLabel"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(errorLabel))
                    .addComponent(instruction)
                    .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 178, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(212, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(instruction)
                .addGap(18, 18, 18)
                .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(errorLabel)
                .addContainerGap(212, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    public String getEditedName() {
        return nameTextField.getText();
    }

    public void setEditedName(String name) {
        nameTextField.setText(name);
    }

    private void nameTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nameTextFieldFocusLost
        // Validate text field to see if it is a URL
        boolean nameValidAfterEditing = isNameValid();
        errorLabel.setVisible(!nameValidAfterEditing);
        if ( nameValidAfterEditing ^ nameValidBeforeEditing ) {
            // Validity has changed
            fireChangeListenerStateChanged();
        }
}//GEN-LAST:event_nameTextFieldFocusLost

    private void nameTextFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nameTextFieldFocusGained
        this.nameValidBeforeEditing = isNameValid();
}//GEN-LAST:event_nameTextFieldFocusGained

    private boolean isNameValid() {
        return nameTextField.getText() != null &&
                nameTextField.getText().length() > 0;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel errorLabel;
    private javax.swing.JLabel instruction;
    private javax.swing.JTextField nameTextField;
    // End of variables declaration//GEN-END:variables

}
